//
//  JXVisitorView.swift
//  WBTest
//
//  Created by Fantasy on 2017/1/23.
//  Copyright © 2017年 Fantasy. All rights reserved.
//

import UIKit
import SnapKit

class JXVisitorView: UIView {
    
    /// 访客视图信息字典
    var visitorInfo: [String : String]? {
        didSet {
            // 取字典信息
            guard let imageName = visitorInfo?["imageName"],
                  let message = visitorInfo?["message"] else {
                return
            }
            
            // 设置消息
            tipLabel.text = message
            
            // 设置图像
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }

// MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
// MARK: - 私有控件
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮罩图片
    lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 小房子
    lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 提示标签
    lazy var tipLabel: UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    
    // 注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    // 登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    
    // 旋转图标动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        // 动画完成不消除, 如果iconView被释放才会一起销毁
        anim.isRemovedOnCompletion = false
        
        // 动画添加到图层上
        iconView.layer.add(anim, forKey: nil)
    }
}

extension JXVisitorView {
    func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        // 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        tipLabel.textAlignment = .center
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(0)
            make.centerY.equalTo(self).offset(-80)
        }
        
        houseIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView.snp.center)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.centerX)
            make.top.equalTo(iconView.snp.bottom)
            make.width.equalTo(236)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp.left)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp.right)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        maskIconView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(20)
            make.size.equalTo(CGSize(width: UIScreen.cz_screenWidth(), height: UIScreen.cz_screenHeight()))
        }
    }
}
